// pages/service/sellCar.js
var $api = require('../../utils/api');
var util = require('../../utils/util');
var area_standard = require('../../utils/areaStandard');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_ver: 1,
    publish_vip_time_status: 1,
    order_no: '',
    publish_id: '',
    checked: false,
    autosize: {
      maxHeight: 50,
      minHeight: 50
    }, //输入框高度
    outsize: [], 
    insize: [], 
    valid_time: [{
        text: '3天',
      },{
        text: '3个月',
      },{
        text: '1年',
      },
    ], 
    outsizePopup: false,
    valid_timePopup:false,
    outsizeCell: false,
    insizeCell: false,
    // 提交数据
    form_brand_cate_id: "",
    form_brand_cate: "",
    form_outsize: "",
    form_outsizeCell: "",
    form_insize: "",
    form_insizeCell: "",
    form_area: "",
    form_remark: "",
    userInfo:[],
  },


  //打开位置选择器
  openCityPopup: function (e) {
    this.setData({
      cityColumns: area_standard.default,
      showCityPopup: true
    })
  },
  onCloseCityPopup: function (e) {
    this.setData({
      showCityPopup: false
    })
  },
  onChangeCityPopup: function (e) {
    let list = e.detail.values;
    //拼接后端接受的区域格式
    let region = list[0].name + "/" + list[1].name + "/" + list[2].name;
    this.setData({
      form_area: region,
      showCityPopup: false
    })
  },

  //打开品牌选择器
  openBrandSelection: function (e) {
    wx.navigateTo({
      url: '../new/brandSelection',
    })
  },

  //打开外观选择器
  outsizePopup: function (e) {
    if(!this.data.form_brand_cate_id)
    {
      wx.showToast({
        title: '请先选择车型',
        icon: 'none',
        duration: 2000
      })
    }
    this.setData({
      outsizePopup: true
    })
    this.get_outsize();
  },
  //关闭外观选择器
  onCloseOutsizePopup: function (e) {
    this.setData({
      outsizePopup: false
    })
  },
  onChangeOutsize: function (e) {
    if(e.detail.value == '自定义')
    {
      this.setData({
        outsizeCell: true
      })
    }
    this.setData({
      form_outsize: e.detail.value,
      outsizePopup: false
    })
  },

  //打开内饰选择器
  insizePopup: function (e) {
    if(!this.data.form_brand_cate_id)
    {
      wx.showToast({
        title: '请先选择车型',
        icon: 'none',
        duration: 2000
      })
    }
    this.setData({
      insizePopup: true
    })
    this.get_insize();
  },
  //关闭外观选择器
  onCloseInsizePopup: function (e) {
    this.setData({
      insizePopup: false
    })
  },
  onChangeInsize: function (e) {
    if(e.detail.value == '自定义')
    {
      this.setData({
        insizeCell: true
      })
    }
    this.setData({
      form_insize: e.detail.value,
      insizePopup: false
    })
  },

  //打开有效期选择器
  valid_timePopup: function (e) {
    this.setData({
      valid_timePopup: true
    })
  },
  //关闭有效期选择器
  onCloseValid_timePopup: function (e) {
    this.setData({
      valid_timePopup: false
    })
  },
  onChangeValid_time: function (e) {
    this.setData({
      form_valid_time: e.detail.value.text,
      valid_timePopup: false
    })
  },

  get_outsize: function (e) {
    let params;
    params = {
      id: this.data.form_brand_cate_id
    };
    $api.get_outsize(params).then(res => {
      if (res.code == 1) {
        if(res.data)
        {
          var outsize = res.data
          outsize.unshift('自定义');
          this.setData({
            outsize: outsize,
          })
        }
      }
    })
  },

  get_insize: function (e) {
    let params;
    params = {
      id: this.data.form_brand_cate_id
    };
    $api.get_insize(params).then(res => {
      if (res.code == 1) {
        if(res.data)
        {
          var insize = res.data
          insize.unshift('自定义');
          this.setData({
            insize: insize,
          })
        }
      }
    })
  },

  // 急寻置顶开关
  switchChange: function(e)
  {
    this.setData({
      checked: e.detail.value,
    })
  },

  //发布车源
  release: function () {
    var form_brand_cate = this.data.form_brand_cate;
    var form_insize = this.data.form_insize;
    var form_outsize = this.data.form_outsize;
    var form_valid_time = this.data.form_valid_time;
    var form_area = this.data.form_area;
    var form_brand_cate_id = this.data.form_brand_cate_id;
    var form_remark = this.data.form_remark;
    var checked = this.data.checked;
    if(form_outsize == '自定义')
    {
      form_outsize = this.data.form_insizeCell;
    }
    if(form_insize == '自定义')
    {
      form_insize = this.data.form_outsizeCell;
    }
    if (form_area != "" && form_brand_cate != "" && form_insize != "" &&
      form_outsize != "" && form_valid_time != "" ) 
    {
      let params = {
        brand_name: form_brand_cate,
        brand_id: form_brand_cate_id,
        outsize: form_outsize,
        insize: form_insize,
        area: form_area,
        valid_time: form_valid_time,
        remark: form_remark,
        top: checked
      }
      $api.add_new_car_publish(params).then(res => {
        if (res.code == 1) {
          this.setData({
            publish_id: res.data.publish_id,
            order_no: res.data.order_no
          })
          wx.showToast({
            title: '提交成功!',
            icon: 'none',
            duration: 2000 //持续的时间
          })
          if(this.data.checked)
          {
            this.sumbit();
          }
          wx.navigateBack({
            delta: 1,
          })
        }
        else
        {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      })
    } else {
      wx.showToast({
        title: '请填写完整信息后提交!',
        icon: 'none',
        duration: 2000 //持续的时间
      })
    }
  },

  
  //电话联系
  contact: function (e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  //置顶支付
  sumbit: function () {
    //关闭所有页面重新登录
    let params={
      order_no:this.data.order_no
    };
    $api.orderPayPublishCaTop(params).then(res=>{
      if(res.code==1){
        wx.requestPayment({
          timeStamp: res.data.timestamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign:res.data.paySign,
          success (res) { 
            wx.navigateBack({
              success: function () {
                wx.showToast({
                  title: '支付成功!',
                  icon: 'success',
                  duration: 2000 //持续的时间
                })
              }
            })
          },
          fail (res) {

           }
        })
      }
   
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  onLoad: function (option) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取userInfo
    $api.userInfo().then(res => {
      if (res.code == 1) {
        this.setData({
          publish_vip_time_status: res.data.publish_vip_time_status,
          is_ver: res.data.data_verification,
          userInfo:res.data
        })
      }
    })
  },

  //跳转到注册页面
  registered: function (e) {
    if(this.data.is_ver == 0 )
    {
      wx.navigateTo({
        url: '../my/registered?order_type=4',
      })
    }
    else
    {
      wx.navigateTo({
        url: '../my/myInfo',
      })
    }
  },
  //跳转到续费页面
  renew: function () {
    wx.navigateTo({
      url: '../my/myInfo',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})